/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.dataload;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.swing.JList;

import fr.upvm.lem3.vm3d.services.parser.Reader;

/**
 *******************************************************************
 *CtfDataLoaderController.java
 *19.02.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
public class CtfDataLoaderController {
	
	private CtfDataLoader ctfDataLoader;
	private DataLoadController dataLoaderController;
	
	public CtfDataLoaderController(CtfDataLoader dataLoader, DataLoadController dataLoaderController){
		this.setCtfDataLoader(dataLoader);
		this.setDataLoaderController(dataLoaderController);
	}


	/**
	 * 
	 */
	public void selectDirectory(String path){
		getDataLoaderController().selectDirectory(path);
	}
	
	/**
	 * 
	 */
	public void selectFileToSave(String path){
		getDataLoaderController().selectFileToSave(path);
	}
	/**
	 * 
	 */
	public void selectFileToLoad(String path) {
		getDataLoaderController().selectFileToLoad(path);
	}
	/**
	 * 
	 */
	
	/**
	 * @return the dataLoaderController
	 */
	/**
	 * @return the ctfDataLoader
	 */
	public CtfDataLoader getCtfDataLoader() {
		return ctfDataLoader;
	}
	
	/**
	 * @param ctfDataLoader the ctfDataLoader to set
	 */
	public void setCtfDataLoader(CtfDataLoader ctfDataLoader) {
		this.ctfDataLoader = ctfDataLoader;
	}
	public DataLoadController getDataLoaderController() {
		return dataLoaderController;
	}

	/**
	 * @param dataLoaderController the dataLoaderController to set
	 */
	public void setDataLoaderController(DataLoadController dataLoaderController) {
		this.dataLoaderController = dataLoaderController;
	}

	
	/**
	 * 
	 * @param selecteddir
	 * @param listCTFs
	 */
	public void handleDirectorySelection(File selecteddir, Vector<String> newData) {
		// Pull the box on the left with the new data. 
		
		//Fill the textfile with the current new directory path
		try {
			getCtfDataLoader().getFieldInputDir().setText(selecteddir.getCanonicalPath());
		} catch (IOException e1) {
			System.out.println(e1.getMessage());
			e1.printStackTrace();
		}
		try {
			getCtfDataLoader().setInputPath(selecteddir.getCanonicalPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File TempFile = new File(selecteddir.getParent() + "\\_Output\\"+ selecteddir.getName() +".lem3");
		String defaultOuputfile;
//		try {
//			defaultOuputfile = TempFile.getCanonicalPath();
//			if(getCtfDataLoader().getFieldOutputFile().getText().equals(new String(""))){
//				getCtfDataLoader().getFieldOutputFile().setText(defaultOuputfile);
//				getCtfDataLoader().setOutputPath(defaultOuputfile);
//			}
//		} catch (IOException e) {
//			System.out.println(e.getMessage());
//			e.printStackTrace();
//		}
		//remove old data and add new one
		((MyListModel<String>)getCtfDataLoader().getListRight().getModel()).clear();
		MyListModel<String> model =  ((MyListModel<String>)getCtfDataLoader().getListLeft().getModel());
		model.clear();
		model. addAll(newData);  // add the data contained in the given directory into the box at the left side
		
		
	}

	/**
	 * 
	 * @param file
	 * @param fileExtension
	 */
	public void handleFileOutputSelection(File file, String fileExtension) {
		String path = null;
		try {
			path = file.getCanonicalPath();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getCtfDataLoader().getFieldOutputFile().setText(new String(path+fileExtension));
		getCtfDataLoader().setOutputPath(new String(path+fileExtension));

	}

	/**
	 * 
	 */
	public void startParsingFiles() {
		
		Vector<String> vec =((MyListModel<String>)getCtfDataLoader().getListRight().getModel()).getData();
		//now start to parse the file into an file3d object
		//first do an initiation of the reader instance and than start with the parsing
		Reader.getInstance().init();
		
		List<File> filesToparse= new Vector<File>();
		
		for (String el: vec){
			filesToparse.add(new File(el));
		}
		getCtfDataLoader().getProgressBar().setMaximum(filesToparse.size());
		Reader.getInstance().addChannelChangedListener(getCtfDataLoader().getProgressBar());
		
		if(!Reader.getInstance().parseCTF((filesToparse))){
			
			System.err.println(" Parsing the giving files has failed");
			
		}
		else{
//			System.err.println(" Z-Dim: "+Reader.getInstance().getFile3d().getTail().length);
			System.out.println(" Parsing seem to be succefull. But it remains slower than expect \n");
		}
		//
		Thread[] threads = new Thread[Thread.activeCount()];
		Thread.enumerate(threads);
		for(Thread ct: threads){
			if(ct.getName().equals("Lem3Loader")){
				ct.interrupt();
				}
		}
	}
	
	/**
	 * stop parsing the file. 
	 * Invoke when the user click interrupt the parse action
	 */
	public void stopParsing(){
		Reader.getInstance().removeChannelChangedListener(getCtfDataLoader().getProgressBar());
	}
		

}
